
import 'package:flutter/material.dart';
import 'dart:convert';
import 'package:jd_shopping/HttpRequest/SpUtils.dart';

class CartsShow with ChangeNotifier{
  
  List _cartList=[];  //状态
  List get cartList=>this._cartList;
  bool _isCheckedAll = false; //状态
  double _totalPrice = 0;
  bool get isCheckedAll => this._isCheckedAll;
  double get totalPrice => this._totalPrice;

  CartsShow(){
    this.init();
  }
  //初始化的时候获取购物车数据
  init() async{
      try{
        List cartListData = json.decode(await SpUtils.get('cartList'));
        this._cartList=cartListData;         
      }catch(e){
        this._cartList=[];
      }
      this._isCheckedAll=this.isCheckAll();
      computeAllPrice();
      notifyListeners();
  }


  itemCountChange() {
    computeAllPrice();
    SpUtils.save("cartList", json.encode(this._cartList));
    notifyListeners();
    
  }


  updateCartList(){
    this.init(); 
  }

  //全选 反选
  checkAll(value) {
    for (var i = 0; i < this._cartList.length; i++) {
      this._cartList[i]["checked"] = value;
    }
    this._isCheckedAll = value;
    computeAllPrice();
    SpUtils.save("cartList", json.encode(this._cartList));
    notifyListeners();
   
  }

  //判断是否全选
  bool isCheckAll() {
    if (this._cartList.length > 0) {
      for (var i = 0; i < this._cartList.length; i++) {
        if (this._cartList[i]["checked"] == false) {
          return false;
        }
      }
      return true;
    }
    return false;
  }


 //监听每一项的选中事件
  itemChage(){
      if(this.isCheckAll()==true){
        this._isCheckedAll=true;
      }else{
         this._isCheckedAll=false;
      }

       computeAllPrice();
       SpUtils.save("cartList", json.encode(this._cartList));
       notifyListeners();
      
  }

  //计算总价
  computeAllPrice() {
    double tempAllPrice = 0;
    for (var i = 0; i < this._cartList.length; i++) {
      if (this._cartList[i]["checked"] == true) {
        tempAllPrice += this._cartList[i]["price"] * this._cartList[i]["count"];
      }
    }
    this._totalPrice = tempAllPrice;
    notifyListeners();
  }


//删除数据
  removeItem() {
              //  1        2
    // ['1111','2222','333333333','4444444444']
    // 错误的写法 
    // for (var i = 0; i < this._cartList.length; i++) {
    //   if (this._cartList[i]["checked"] == true) {
    //      this._cartList.removeAt(i);
    //   }
    // }
    List tempList=[];
    for (var i = 0; i < this._cartList.length; i++) {
      if (this._cartList[i]["checked"] == false) {
         tempList.add(this._cartList[i]);
      }
    }
    this._cartList=tempList;
    //计算总价
    this.computeAllPrice();
    this.itemChage();
    SpUtils.save("cartList", json.encode(this._cartList));
    notifyListeners();
  }

}